package com.ruoyi.shop.service.impl;

import java.math.BigDecimal;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.shop.domain.ShopGoods;
import com.ruoyi.shop.mapper.ShopGoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.shop.mapper.ShopGoodsCartMapper;
import com.ruoyi.shop.domain.ShopGoodsCart;
import com.ruoyi.shop.service.IShopGoodsCartService;

/**
 * 购物车Service业务层处理
 * 
 * @author jiaodacailei
 * @date 2022-06-12
 */
@Service
public class ShopGoodsCartServiceImpl implements IShopGoodsCartService 
{
    @Autowired
    private ShopGoodsCartMapper shopGoodsCartMapper;

    /**
     * 查询购物车
     * 
     * @param id 购物车主键
     * @return 购物车
     */
    @Override
    public ShopGoodsCart selectShopGoodsCartById(Long id)
    {
        return shopGoodsCartMapper.selectShopGoodsCartById(id);
    }

    /**
     * 查询购物车列表
     * 
     * @param shopGoodsCart 购物车
     * @return 购物车
     */
    @Override
    public List<ShopGoodsCart> selectShopGoodsCartList(ShopGoodsCart shopGoodsCart)
    {
        return shopGoodsCartMapper.selectShopGoodsCartList(shopGoodsCart);
    }

    /**
     * 新增购物车
     * 
     * @param shopGoodsCart 购物车
     * @return 结果
     */
    @Override
    public int insertShopGoodsCart(ShopGoodsCart shopGoodsCart)
    {
        shopGoodsCart.setCreateTime(DateUtils.getNowDate());
        return shopGoodsCartMapper.insertShopGoodsCart(shopGoodsCart);
    }

    /**
     * 修改购物车
     * 
     * @param shopGoodsCart 购物车
     * @return 结果
     */
    @Override
    public int updateShopGoodsCart(ShopGoodsCart shopGoodsCart)
    {
        shopGoodsCart.setUpdateTime(DateUtils.getNowDate());
        return shopGoodsCartMapper.updateShopGoodsCart(shopGoodsCart);
    }

    /**
     * 批量删除购物车
     * 
     * @param ids 需要删除的购物车主键
     * @return 结果
     */
    @Override
    public int deleteShopGoodsCartByIds(Long[] ids)
    {
        return shopGoodsCartMapper.deleteShopGoodsCartByIds(ids);
    }

    /**
     * 删除购物车信息
     * 
     * @param id 购物车主键
     * @return 结果
     */
    @Override
    public int deleteShopGoodsCartById(Long id)
    {
        return shopGoodsCartMapper.deleteShopGoodsCartById(id);
    }

    @Override
    public void insertShopGoodsCartByGoods(long goodsId) {
        ShopGoodsCart search = new ShopGoodsCart();
        search.setGoodsId(goodsId);
        List<ShopGoodsCart> shopGoodsCarts = shopGoodsCartMapper.selectShopGoodsCartList(search);
        if (shopGoodsCarts.size() == 0) {
            ShopGoodsCart cart = new ShopGoodsCart();
            cart.setGoodsId(goodsId);
            cart.setGoodsCount(BigDecimal.valueOf(1L));
            cart.setIdx(0);
            cart.setCreateUpdateInfo();
            shopGoodsCartMapper.insertShopGoodsCart(cart);
        } else {
            ShopGoodsCart cart = shopGoodsCarts.get(0);
            cart.setUpdateInfo();
            cart.setGoodsCount(BigDecimal.valueOf(cart.getGoodsCount().longValue() + 1));
            shopGoodsCartMapper.updateShopGoodsCart(cart);
        }
    }
}
